﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;

public partial class ControlBaoCaoDiemDanh : System.Web.UI.UserControl
{
    DataTable dt;
    static DataTable dsTVV;
    static string error = "";
    MethodClassCommon common;
    static string usertype = "";
    ClassExecute.ClassExecute Cl;
    string str = BCDataCenter.Configuration.CurrentConnectionString;
    BCDataCenter.DataAccessLayer.DAL dal;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            
            displayThang();
            displayNam();
            displayTTPP();
            try
            {
                usertype = checkUserType();
                DropVung.SelectedValue = Session["Sitecode"].ToString();
                if ((usertype == "sa") || (usertype == "us"))
                {
                    DropVung.Enabled = false;
                }

            }
            catch (Exception ex)
            {
                uscMsgBox1.AddMessage(ex.Message, uscMsgBox.enmMessageType.Error);
            }
        }
        else
        {
            try
            {
                ASPxPivotGrid1.DataSource = dsTVV;
                ASPxPivotGrid1.DataBind();
            }
            catch
            {

                return;
            }
        }
    }
    public string checkUserType()
    {
        common = new MethodClassCommon();
        string type = common.GetDataDepend("GET_USERTYPE", "UserName", Session["username"].ToString());
        return type;
    }
    void displayTTPP()
    {
        dal = new BCDataCenter.DataAccessLayer.DAL();
        dt = dal.get_data_table("GET_SITENAME_ALL");
        DropVung.DataSource = dt;
        DropVung.DataTextField = "sitename";
        DropVung.DataValueField = "Sitecode";
        DropVung.DataBind();

    }
    void displayThang()
    {
        for (int i = 1; i < 13; i++)
        {
            DropThang.Items.Add(Convert.ToString(i));
        }
        DropThang.SelectedValue = DateTime.Now.Month.ToString();
    }
    void displayNam()
    {
        double years =double.Parse(DateTime.Now.Year.ToString());
        DropNam.Items.Add(Convert.ToString(years));
        DropNam.Items.Add(Convert.ToString(years-1));
        DropNam.Items.Add(Convert.ToString(years - 2));
        DropNam.SelectedValue = DateTime.Now.Year.ToString();
    }
    protected void XemBaoCao_Click1(object sender, EventArgs e)
    {
        try
        {
            Cl = new ClassExecute.ClassExecute();
            string MmYyyy = "";
            if ((DropThang.SelectedItem.ToString() != "10") || (DropThang.SelectedItem.ToString() != "11") || (DropThang.SelectedItem.ToString() != "12"))
            {
                MmYyyy = "0" + DropThang.SelectedItem.ToString();
            }
            else
            {
                MmYyyy =DropThang.SelectedItem.ToString();
            }
            dsTVV = Cl.Get_Data_DataTable(str, ref error, "MK_GET_BAOCAO_DIEMDANH_TVV", CommandType.StoredProcedure, new SqlParameter("@MmYyyy", MmYyyy + DropNam.SelectedItem.ToString()), new SqlParameter("@sitecode", DropVung.SelectedValue.ToString()));
            if (dsTVV.Rows.Count > 0)
            {
            ASPxPivotGrid1.Fields.Clear();
                foreach (DataColumn col in dsTVV.Columns)
                {
                    if (col.ColumnName == "Ngay")
                    {
                        ASPxPivotGrid1.Fields.Add(col.ColumnName.ToString(), DevExpress.XtraPivotGrid.PivotArea.ColumnArea);
                    }
                    else if (col.ColumnName == "isPresent")
                    {
                        ASPxPivotGrid1.Fields.Add(col.ColumnName.ToString(), DevExpress.XtraPivotGrid.PivotArea.DataArea);
                    }
                    else
                    {
                        ASPxPivotGrid1.Fields.Add(col.ColumnName.ToString(), DevExpress.XtraPivotGrid.PivotArea.RowArea);
                    }
                }
                ASPxPivotGrid1.OptionsView.DataHeadersDisplayMode = DevExpress.Web.ASPxPivotGrid.PivotDataHeadersDisplayMode.Popup;
                ASPxPivotGrid1.OptionsView.DataHeadersPopupMinCount = 1;
                ASPxPivotGrid1.DataSource = dsTVV;
                ASPxPivotGrid1.DataBind();
            }
            else
            {
                uscMsgBox1.AddMessage("Không có dữ liệu", uscMsgBox.enmMessageType.Error);
            }
        }
        catch (Exception ex)
        {
           
            uscMsgBox1.AddMessage(ex.Message, uscMsgBox.enmMessageType.Error);
        }
    }
    protected void Export_Click(object sender, EventArgs e)
    {
        using (MemoryStream filestream = new MemoryStream())
        {
            string contentType = "application/ms-excel";
            string fileName =DropThang.SelectedItem.ToString() + DropNam.SelectedItem.ToString()  + "_BaoCaoDiemDanhTVV.xls";
            ASPxPivotGridExporter1.ExportToXls(filestream);
            byte[] buffer = filestream.GetBuffer();
            Response.Clear();
            Response.Buffer = false;
            Response.AppendHeader("Content-Type", contentType);
            Response.AppendHeader("Content-Transfer-Encoding", "binary");
            Response.AppendHeader("Content-Disposition", "attachment;filename=" + fileName);
            Response.BinaryWrite(buffer);
            Response.End();
        }
    }
}
